import { Component, OnInit } from '@angular/core';
import { ProjectService } from 'src/app/core/service/project.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-jar-list',
  templateUrl: './jar-list.component.html',
  styleUrls: ['./jar-list.component.scss']
})
export class JarListComponent implements OnInit {
  projectUuid = '';

  allChecked = false;
  indeterminate = false;
  displayData = [];

  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页

  dataSet = [];       // 数据集合

  loading = true;     // 加载
  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  searchGenderList: string[] = [];

  sort(sort: { key: string, value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
  }

  constructor(
    private projectService: ProjectService,
    private activatedRoute: ActivatedRoute
  ) { }

  /**
   *
   * @param reset 获取数据
   */
  getJarPage(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.projectService.getJarPage(this.pageIndex, this.pageSize, this.projectUuid)
      .subscribe((JarData) => {
        this.dataSet = JarData.data;
        this.total = JarData.total;
      });
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }

  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(params => {
      this.projectUuid = params['projectUuid'];
      console.log('=== janine.ngOnInit().projectUuid ===>', this.projectUuid);
      this.getJarPage();
    });
  }
}
